<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Order extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $page = $request->get('page', 1, 'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit', 10, 'intval');
            $name = $request->get('name', '', 'trim');
            $transaction = $request->get('transaction', '', 'trim');
            $order = Db('order');
            if ($name != '') {
                $course_ids = Db('course')->where('name', 'like', '%' . $name . '%')->column('course_id');
                $course_ids[] = 0;
                $order->where('course_id','in',$course_ids);
            }
            if ($transaction != '') {
                $order->where('transaction',$transaction);
            }
            $count = $order->count();
            $list = $order
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function editOrder(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $order_id = $request->post('order_id', 0, 'intval');
            $status = $request->post('status', 0, 'intval');
            if (\app\model\Order::setStatus($order_id, $status)) {
                $data['description'] = $request->post('description', '', 'trim');
                if (false === Db('order')->where('order_id', $order_id)->update($data)) {
                    return ['code' => 1, 'msg' => '操作失败'];
                } else {
                    return ['code' => 0, 'msg' => '操作成功'];
                }
            } else {
                return ['code' => 1, 'msg' => '操作失败'];
            }
        } else {
            $order_id = $request->get('id', 0, 'intval');
            $info = Db('order')
                ->alias('o')
                ->join('user u', 'o.user_id = u.user_id', 'LEFT')
                ->where('o.order_id', $order_id)
                ->field('o.*,u.nickname,u.avatar')
                ->find();
            $member = Db('orderMember')->where('order_id', $order_id)->select();
            $this->assign('info', $info);
            $this->assign('member', $member);
            return $this->fetch();
        }
    }


    public function addOrderMember(Request $request)
    {
        if ($request->isPost()) {
            $order_id = $request->post('order_id', 0, 'intval');
            $info = Db('order')
                ->where('order_id', $order_id)
                ->find();
            $order_member = [
                'order_id' => $info['order_id'],
                'course_id' => $info['order_id'],
                'company' => $info['company'],
                'contact' => $request->post('contact', '', 'trim'),
                'phone' => $request->post('phone', '', 'trim'),
                'sex' => $request->post('sex', 0, 'intval'),
                'position' => $request->post('position', '', 'trim'),
                'address' => $info['address'],
            ];
            if ($order_member['contact'] == '') {
                return ['code' => 1, 'msg' => '请填写参会人姓名'];
            }
            if ($order_member['phone'] == '') {
                return ['code' => 1, 'msg' => '请填写参会人电话'];
            }
            if ($order_member['position'] == '') {
                return ['code' => 1, 'msg' => '请填写参会人职务'];
            }
            if (!Db('orderMember')->insert($order_member)) {
                return ['code' => 0, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $order_id = $request->get('id', 0, 'intval');
            $info = Db('order')
                ->where('order_id', $order_id)
                ->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    public function deleteOrderMember(Request $request)
    {
        $member_id = $request->get('id', 0, 'intval');
        if (false === Db('orderMember')->where('member_id', $member_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }
}